﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;

namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class UAgentMacStockDetailNGAdd : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改, Permission.新增 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "UAgentMacList";
            }

        }
        /// <summary>
        /// 操作类型,ADD,UPDATE
        /// </summary>
        protected string Action
        {
            get
            {
                return GetRequestValue<string>("action", "add");
            }
        }
        protected int UASID
        {
            get
            {
                return GetRequestValue<int>("UASID", 0);
            }
        }

        private UAgentMacStockInfo _UAgentMacStock = null;
        protected UAgentMacStockInfo UAgentMacStockObj
        {
            get
            {
                if (_UAgentMacStock == null)
                    _UAgentMacStock = UAgentMacStockController.GetById(UASID);

                return _UAgentMacStock == null ? new UAgentMacStockInfo() { IntoStockDate = "", MacType = "", AgentName = HasPower(Permission.经销商平台_经销商) ? base.User.Department : "" } : _UAgentMacStock;
            }
        }

        protected int UASDID { get { return GetRequestValue<int>("UASDID", 0); } }
        private UAgentMacStockDetailInfo _UAgentMacStockDetail = null;
        protected UAgentMacStockDetailInfo UAgentMacStockDetailObj
        {
            get
            {
                if (_UAgentMacStockDetail == null)
                    _UAgentMacStockDetail = UAgentMacStockController.UAgentMacStockDetail_GetByUASDID(UASID, UASDID);

                return _UAgentMacStockDetail == null ? new UAgentMacStockDetailInfo() { CheckDate = "" } : _UAgentMacStockDetail;
            }
        }


        private List<UAgentMacStockDetailNGInfo> _CurrUAgentMacNewNGInfo = null;
        /// <summary>
        /// 当前ng内容
        /// </summary>
        protected List<UAgentMacStockDetailNGInfo> CurrUAgentMacNewNG
        {
            get
            {
                if (_CurrUAgentMacNewNGInfo == null)
                    _CurrUAgentMacNewNGInfo = UAgentMacStockController.UAgentMacStockDetailNG_GetAllByUASDID(UASDID);
                return _CurrUAgentMacNewNGInfo;
            }
        }

        private List<UAgentMacStockDetailTempleteInfo> _AllUAgentMacNewTemplete = null;
        /// <summary>
        /// 所有可选的NG内容
        /// </summary>
        protected List<UAgentMacStockDetailTempleteInfo> AllUAgentMacNewTemplete
        {
            get
            {
                if (_AllUAgentMacNewTemplete == null)
                    _AllUAgentMacNewTemplete = UAgentMacStockController.UAgentMacStockDetailTemplete_GetAll();
                return _AllUAgentMacNewTemplete;
            }
        }
        


        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "update")
            {
                updatemenu();
            }

            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
               addmenu();
            }


            this.rpMenu.DataSource = AllUAgentMacNewTemplete.FindAll(X => X.ParTempID == 0 && X.ClassType==0);
            this.rpMenu.DataBind();

            this.rpNGImg.DataSource = AllUAgentMacNewTemplete.FindAll(X => X.ParTempID == 0 && X.ClassType == 1);
            this.rpNGImg.DataBind();

        }

        protected string GetSubTrainHtml(object _ParTempID)
        {
            /*
                <tr>
                    <td><input type="checkbox" name="checkselected"  value="<%# Eval("TrainId")%>" <%# GetChecked(Eval("TrainId")) %>  /></td>
                    <td><%# Eval("TrainDept")%></td>
                    <td><%# Eval("TrainContent")%></td>
                    <td><input type="text"  size="3" name="ClassHour" value="<%# GetClassHour(Eval("TrainId"),Eval("ClassHour")) %>"  /></td>
                    <td><input type="text" size="3" name="Unit" value="<%# GetUnit(Eval("TrainId"),Eval("Unit")) %>"  /></td>
                </tr>
             */

            int ParTempID = Convert.ToInt32(_ParTempID);
            string returnstr = "";


            List<UAgentMacStockDetailTempleteInfo> list = AllUAgentMacNewTemplete.Where<UAgentMacStockDetailTempleteInfo>(e => e.ParTempID == ParTempID).ToList();

            StringBuilder sb = new StringBuilder();
            foreach (UAgentMacStockDetailTempleteInfo itm in list)
            {
                UAgentMacStockDetailNGInfo selObj = CurrUAgentMacNewNG.FirstOrDefault<UAgentMacStockDetailNGInfo>(e => e.TempID == itm.TempID);
                selObj = selObj == null ? new UAgentMacStockDetailNGInfo() : selObj;
                sb.AppendFormat(@"
                 <tr>
                    <td align='right' style=' text-align:right;'>
                     <input type='hidden' name='TempID'  value='{0}'/>
                     <input type='checkbox' name='checkselected' value='{0}' {1}/>
                    </td>                   
                    <td>{2}</td>
                 
                    <td><input type='text'   name='NGSolution' value='{3}' style='width:90%'  /></td>
                    
                </tr>",
                      itm.TempID, selObj.ID > 0 ? "checked" : "", itm.Title, selObj.NGSolution
                );
            }

            

            return sb.ToString();
        }
        protected string GetSubTrainHtml2(object _ParTempID)
        {
            int ParTempID = Convert.ToInt32(_ParTempID);
            string returnstr = "";
            List<UAgentMacStockDetailTempleteInfo> list = AllUAgentMacNewTemplete.Where<UAgentMacStockDetailTempleteInfo>(e => e.ParTempID == ParTempID).ToList();
            StringBuilder sb = new StringBuilder();
            foreach (UAgentMacStockDetailTempleteInfo itm in list)
            {
                UAgentMacStockDetailNGInfo selObj = CurrUAgentMacNewNG.FirstOrDefault<UAgentMacStockDetailNGInfo>(e => e.TempID == itm.TempID);
                selObj = selObj == null ? new UAgentMacStockDetailNGInfo() : selObj;
                sb.AppendFormat(@"
                     <div style='float:left; width:160px; border:1px solid #000; margin-right:20px; margin-bottom:20px;'>
                    <div style=' line-height:25px; height:25px; border-bottom:1px solid #000; padding:0px 5px;'>
                     <input type='hidden' name='TempID2'  value='{0}'/>{1}</div>
                    <div style='padding:5px;'>
                        <a  class='imgUAgentMacStockDetailNGAddShow' href='{2}'><img id='imgUAgentMacStockDetailNGAddShow{0}' src='{2}' Width='150' Height='150'/></a>
                    </div>
                    <div style='padding:5px;'>
                        <input type='hidden'   name='NGImg2' id='imgUAgentMacStockDetailNGAddNGImg{0}' value='{2}'  />
                        <input type='file' name='filedata' id='imgAgentMacStockDetailNGAddAdd{0}' upload='upload' clientId='imgUAgentMacStockDetailNGAddShow{0},imgUAgentMacStockDetailNGAddNGImg{0}' dirtype='6' foldername='{3}' onUploadSuccess='onUAgentMacStockDetailNGAddUploadImgSuccess' fileTypeDesc='只能上传图片' fileTypeExts='*.jpg;*.gif;*.jpeg;*.png;*.bmp' fileSizeLimit='1MB'/>
                     
                    </div>
                    <div style='padding:5px;'>
                        <a href='#' onclick=""onUAgentMacStockDetailNGAddDeleteImg('imgUAgentMacStockDetailNGAddNGImg{0}','imgUAgentMacStockDetailNGAddShow{0}')"">删除</a>
                    </div>
                </div> ",
                    itm.TempID, itm.Title, selObj.NGImg, base.User.Department);
            }
            return sb.ToString();
        }
        protected void addmenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] checkselecteds = GetRequestValue<string>("checkselected", "").Split(caSplit);
            string[] TempIDs = GetRequestValue<string>("TempID", "").Split(caSplit);
            string[] NGSolutions = GetRequestValue<string>("NGSolution", "").Split(caSplit);
            //string[] NGImgs = GetRequestValue<string>("NGImg", "").Split(caSplit);

            UAgentMacStockDetailInfo obj = Request.SerializableObject<UAgentMacStockDetailInfo>();


            List<UAgentMacStockDetailNGInfo> list = new List<UAgentMacStockDetailNGInfo>();
            for (int index = 0; index < checkselecteds.Count(); index++)
            {
                if (string.IsNullOrEmpty(checkselecteds[index]))
                    continue;

                UAgentMacStockDetailNGInfo itm = new UAgentMacStockDetailNGInfo();
                itm.TempID = Convert.ToInt32(checkselecteds[index]);               
                itm.NGSolution = NGSolutions[index];
                list.Add(itm);
            }
            string[] TempID2s = GetRequestValue<string>("TempID2", "").Split(caSplit);
            string[] NGImg2s = GetRequestValue<string>("NGImg2", "").Split(caSplit);
            for (int index = 0; index < TempID2s.Count(); index++)
            {
                UAgentMacStockDetailNGInfo itm = new UAgentMacStockDetailNGInfo();
                itm.TempID = Convert.ToInt32(TempID2s[index]);
                itm.NGImg = NGImg2s[index];
                list.Add(itm);
            }

            string r = UAgentMacStockController.UAgentMacStockDetailNG_Add(obj, list);
            if (r == "")
            {
                SuccessJson("成功保存");

            }
            else {
                ErrorJson("添加失败"+r);
            }


        }

        private void updatemenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] checkselecteds = GetRequestValue<string>("checkselected", "").Split(caSplit);
            string[] TempIDs = GetRequestValue<string>("TempID", "").Split(caSplit);
            string[] NGSolutions = GetRequestValue<string>("NGSolution", "").Split(caSplit);
            //string[] NGImgs = GetRequestValue<string>("NGImg", "").Split(caSplit);

            UAgentMacStockDetailInfo obj = Request.SerializableObject<UAgentMacStockDetailInfo>();


            List<UAgentMacStockDetailNGInfo> list = new List<UAgentMacStockDetailNGInfo>();
            for (int index = 0; index < checkselecteds.Count(); index++)
            {
                if (string.IsNullOrEmpty(checkselecteds[index]))
                    continue;

                UAgentMacStockDetailNGInfo itm = new UAgentMacStockDetailNGInfo();
                itm.TempID = Convert.ToInt32(checkselecteds[index]);
                itm.NGSolution = NGSolutions[index];
                list.Add(itm);
            }
            string[] TempID2s = GetRequestValue<string>("TempID2", "").Split(caSplit);
            string[] NGImg2s = GetRequestValue<string>("NGImg2", "").Split(caSplit);
            for (int index = 0; index < TempID2s.Count(); index++)
            {
                UAgentMacStockDetailNGInfo itm = new UAgentMacStockDetailNGInfo();
                itm.TempID = Convert.ToInt32(TempID2s[index]);
                itm.NGImg = NGImg2s[index];
                list.Add(itm);
            }

            string r = UAgentMacStockController.UAgentMacStockDetailNG_Update(obj, list);
            if (r == "")
            {
                SuccessJson("成功保存",this.DWZRef);

            }
            else
            {
                ErrorJson("修改失败" + r);
            }
        }
    }
}